/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header$
 * $Id$
 * $Author$
 * $Date$
 * $Revision$
 */
package com.elancom.framework.web.tags;

import javax.servlet.jsp.JspException;

import com.elancom.framework.context.CurrentContext;
import com.elancom.framework.context.UserContext;
import com.elancom.framework.web.ServletUtils;

/**
 * @author elancom
 * @date 2012-7-25
 */
public class UserContextTag extends AbstractTag {
	private static final String DEFAULT_NAME = "userContext";

	private String scope;

	private String name;

	private String property;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
	 */
	@Override
	public int doEndTag() throws JspException {
		if (name == null) {
			name = DEFAULT_NAME;
		}

		UserContext userContext = (UserContext) pageContext.getAttribute(name);
		if (userContext == null) {
			userContext = CurrentContext.getContext().getUserContext();
		}

		if (userContext == null) {
			return super.doEndTag();
		}

		if (property != null) {
			ServletUtils.write(pageContext, getPropertyValue(userContext));
		} else {
			if ("request".equals(scope)) {
				pageContext.getRequest().setAttribute(name, userContext);
			} else {
				pageContext.setAttribute(name, userContext);
			}
		}

		return super.doEndTag();
	}

	/**
	 * @param userContext
	 * @return
	 * 
	 */
	private String getPropertyValue(UserContext userContext) {
		// use reflect ? no no no !
		String propertyValue = null;

		if ("userName".equals(property)) {
			propertyValue = userContext.getUserAccount();
		} else if ("userCode".equals(property)) {
			propertyValue = userContext.getUserCode();
		} else if ("userNickName".equals(property)) {
			propertyValue = userContext.getUserNickName();
		} else if ("userMobile".equals(property)) {
			propertyValue = userContext.getUserMobile();
		} else if ("userEmail".equals(property)) {
			propertyValue = userContext.getUserEmail();
		} else if ("userAccount".equals(property)) {
			propertyValue = userContext.getUserAccount();
		}

		return propertyValue != null ? propertyValue : "";
	}

	/**
	 * @return the scope
	 */
	public String getScope() {
		return scope;
	}

	/**
	 * @param scope
	 *            the scope to set
	 */
	public void setScope(String scope) {
		this.scope = scope;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.jsp.tagext.TagSupport#release()
	 */
	@Override
	public void release() {
		this.scope = null;
		this.name = null;
		this.property = null;
		super.release();
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the property
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property
	 *            the property to set
	 */
	public void setProperty(String property) {
		this.property = property;
	}
}
